package com.oscar.ehome.view.konwledge;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.oscar.ehome.R;
import com.oscar.ehome.base.fragment.BaseFragment;
import com.oscar.ehome.bean.knowpromblebean.KnowlePrombleBean;
import com.oscar.ehome.bean.pagebean.ClassRoomPrombleBean;
import com.oscar.ehome.presenter.ClassRoomPresenter;
import com.oscar.ehome.presenter.KnowelgePresenter;
import com.oscar.ehome.utils.pullrefresh.PullToRefreshBase;
import com.oscar.ehome.utils.pullrefresh.PullToRefreshListView;
import com.oscar.ehome.utils.system.SystemConfigFactory;
import com.oscar.ehome.utils.util.CommonUtils;
import com.oscar.ehome.utils.util.ToastUtil;
import com.oscar.ehome.utils.view.progressview.ProgressFrameLayout;
import com.oscar.ehome.view.konwledge.view.KnowprombleView;
import com.oscar.ehome.view.loginview.LoginActivity;
import com.oscar.ehome.view.pageview.adapter.ClassRoomPrombleAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by juxing
 * author: MengWanhe
 * date: On 2017/9/20
 * 已回答问题页
 */

public class KnowokPrombleFragment extends BaseFragment<KnowprombleView, KnowelgePresenter> implements AdapterView.OnItemClickListener, KnowprombleView,
        PullToRefreshBase.OnRefreshListener, ProgressFrameLayout.AgainRequestListener {

    public View view;
    Unbinder unbinder;

    /**
     *
     */
    @BindView(R.id.oknow_listview)
    PullToRefreshListView oknowListview;
    /**
     *
     */
    @BindView(R.id.oknow_fragment)
    ProgressFrameLayout oknowFragment;

    private int page = 1;//页数
    private int page_size = 20;//每页的条数
    private String token = "";
    private String s = "1";

    KnowelgePresenter presenter;
    ClassRoomPrombleAdapter adapter;
    List<ClassRoomPrombleBean.PrombleListBean> list = new ArrayList<>();


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_okknowpromble, null);
        unbinder = ButterKnife.bind(this, view);
        initView();
        onClick();
        initData();
        return view;
    }

    @Override
    protected KnowelgePresenter createPresenter() {
        presenter = new KnowelgePresenter(this);
        return presenter;
    }

    public void initView() {
        token = SystemConfigFactory.getInstance(getActivity()).getSystemConfig().getToken();

        adapter = new ClassRoomPrombleAdapter(getActivity(), list);
        oknowListview.setAdapter(adapter);
    }

    /**
     * 加载数据
     */
    public void initData() {
        if (isNetWork()) {
            oknowFragment.stopRefresh();
            oknowListview.setVisibility(View.VISIBLE);
            presenter.getPrombleList(token, "2", s, "20");
            showLoading();
        } else {
            oknowFragment.setVisibility(View.VISIBLE);
            oknowFragment.setLoadFail();
            oknowListview.setVisibility(View.GONE);
        }
    }

    public void onClick() {
        oknowListview.setPullLoadEnabled(true);
        oknowListview.setOnRefreshListener(this);
        oknowListview.setOnItemClickListener(this);
        oknowFragment.setAgainRequestListener(this);
    }

    /**
     * 判断是否有网
     */
    public boolean isNetWork() {
        boolean netWork = true;
        if (!CommonUtils.isNetConn(getActivity())) {
            netWork = false;
            return netWork;
        }
        return netWork;
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }

    @Override
    public void answerPromble(String bean) {

    }

    @Override
    public void getKnowPrombles(ClassRoomPrombleBean result) {
        dismissLoading();
        oknowListview.onPullDownRefreshComplete();
        oknowListview.onPullUpRefreshComplete();
        if (result != null) {
            if (result.getList() != null) {
                if (page == 1) {
                    list.clear(); // 将原有的内容清空
                }
                list = result.getList();
                adapter.setLists(list);
            }
        } else {
            oknowListview.setVisibility(View.GONE);
            oknowFragment.setNoResult();
        }
    }

    @Override
    public void erroy() {
        dismissLoading();
        oknowListview.onPullDownRefreshComplete();
        oknowListview.onPullUpRefreshComplete();
        ToastUtil.show(getActivity(), getString(R.string.erry));
        oknowFragment.setLoadFail();
        oknowListview.setVisibility(View.GONE);
    }

    @Override
    public void erry(String result) {
        dismissLoading();
        oknowListview.onPullDownRefreshComplete();
        oknowListview.onPullUpRefreshComplete();
        ToastUtil.show(getActivity(), result);
        if (result.contains("过期")) {
            getActivity().getSharedPreferences("appbase", Context.MODE_PRIVATE).edit().clear().commit();
            SystemConfigFactory.getInstance(getActivity()).getSystemConfig().SetLogin(false);
            SystemConfigFactory.getInstance(getActivity()).getSystemConfig().setIsFirst(true);
            Intent intent = new Intent(getActivity(), LoginActivity.class);
            startActivity(intent);
            getActivity().finish();
        } else {
            oknowFragment.setLoadFail();
            oknowListview.setVisibility(View.GONE);
        }
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        page = 1;
        s = String.valueOf(page);
        initData();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        page++;
        s = String.valueOf(page);
        initData();
    }

    @Override
    public void againRequest() {
        if (isNetWork()) {
            initData();
        } else {
            ToastUtil.show(getActivity(), R.string.pull_to_refresh_network_error);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

}
